package com.system.part.web;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.system.common.util.ExtAjaxPartResponse;
import com.system.common.util.ExtAjaxResponse;
import com.system.common.util.ExtPageable;
import com.system.employee.entity.Employee;
import com.system.part.entity.Part;
import com.system.part.entity.dto.PartDTO;
import com.system.part.service.IPartService;


@Controller
@RequestMapping("/part")
public class PartController 
{
	@Autowired
	private IPartService partService;
	@RequestMapping("/saveOrUpdate")
	public @ResponseBody ExtAjaxResponse saveOrUpdate(Part part) 
	{
		try {
			if(part.getPartId()==null) {
			partService.saveOrUpdate(part);
			return new ExtAjaxResponse(true,"部门增加成功");
			}else {			
				partService.saveOrUpdate(part);
			return new ExtAjaxResponse(true,"部门信息更新成功");
			}
		} catch (Exception e) {
			return new ExtAjaxResponse(false,"部门信息更新失败");
		}
	}
	@RequestMapping("/delete")
	public @ResponseBody ExtAjaxResponse Delete(@RequestParam Long partId) {
		try {
			Part part = partService.findOne(partId);
			partService.delete(part);
			return new ExtAjaxResponse(true,"删除成功");
		} catch (Exception e) {
			return new ExtAjaxResponse(false,"删除失败!部门有员工、岗位等,请先删掉对应员工、岗位");
		}
	}
	@RequestMapping("/deleteParts")
	public @ResponseBody ExtAjaxResponse Deletes(@RequestParam Long[] ids) {
		try {
			List<Long> lists = Arrays.asList(ids);
			if (null!=lists) {
				partService.delete(lists);
			}
			return new ExtAjaxResponse(true,"删除成功");
		} catch (Exception e) {
			return new ExtAjaxResponse(false,"删除失败!部门有员工、岗位等,请请先删掉对应员工、岗位");
		}	
	}
	@RequestMapping("/findOne")
	public @ResponseBody Part findOne(@RequestParam Long partId) 
	{
		Part part =  partService.findOne(partId);
		
		return part;
	}
	
	
	@RequestMapping("/findAll")
	public @ResponseBody ExtAjaxPartResponse findAll() 
	{
		 List<Part> lists =  partService.findAll();
		
		return new ExtAjaxPartResponse(lists);
	}
	@RequestMapping("/findPage")
	public @ResponseBody Page<Part> findPage(PartDTO partDTO,ExtPageable pageable) 
	{
		Page<Part> page =  partService.findAll(PartDTO.getSpecification(partDTO), pageable.getPageable());
		
		return page;
	}
	@RequestMapping("/findEmployee")
	public @ResponseBody List<Employee> findEmployee(Long partId){
		List<Employee> employeeList=partService.findEmployee(partId);
		return employeeList;
		
	}
}
